package com.wee.entity;

import com.alibaba.sdk.android.oss.common.OSSConstants;
import com.alibaba.sdk.android.oss.common.auth.OSSCredentialProvider;
import com.alibaba.sdk.android.oss.common.auth.OSSFederationCredentialProvider;
import com.alibaba.sdk.android.oss.common.auth.OSSFederationToken;
import com.alibaba.sdk.android.oss.common.utils.IOUtils;

import org.json.JSONObject;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
/**阿里图片上传保密措施**/
public class Tools {
	public static final String ENDPOINT="http://oss-cn-beijing.aliyuncs.com";
	
	public static final String TOKEN_URL = "http://apibeta.aircoach.cn/v2/get_aliyun_oss_token?token=123";

	public  static final String testBucket = "aircoach-test";
	
	public static final	OSSCredentialProvider credetialProvider = new OSSFederationCredentialProvider() {
	    @Override
	    public OSSFederationToken getFederationToken() {
	        try {
	            URL stsUrl = new URL(Tools.TOKEN_URL);
	            HttpURLConnection conn = (HttpURLConnection) stsUrl.openConnection();
	            InputStream input = conn.getInputStream();
	            String jsonText = IOUtils.readStreamAsString(input, OSSConstants.DEFAULT_CHARSET_NAME);
	            JSONObject jsonObjs = new JSONObject(jsonText);
	            String ak = jsonObjs.getString("AccessKeyId");
	            String sk = jsonObjs.getString("AccessKeySecret");
	            String token = jsonObjs.getString("SecurityToken");
	            String expiration = jsonObjs.getString("Expiration");
	            return new OSSFederationToken(ak, sk, token, expiration);
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	        return null;
	    }
	};


	
    
}
